package xaj.scc.ast;

import polyglot.ast.Node;
import polyglot.ext.jl.ast.Node_c;
import polyglot.util.CodeWriter;
import polyglot.util.Position;
import polyglot.visit.NodeVisitor;
import polyglot.visit.PrettyPrinter;

public class ProductionElement_c extends Node_c implements ProductionElement {

	private Factor factor;
	private SemanticAction action;

	public ProductionElement_c(Position pos, Factor factor,
			SemanticAction action) {
		super(pos);
		this.factor = factor;
		this.action = action;

	}

	public Factor getFactor() {
		return factor;
	}

	public SemanticAction getSemanticAction() {
		return action;
	}

	public ProductionElement setFactor(Factor factor) {
		ProductionElement_c n = (ProductionElement_c) copy();
		n.factor = factor;
		return n;
	}

	protected ProductionElement_c reconstruct(Factor factor,
			SemanticAction action) {
		if (this.action != action || factor != this.factor) {
			ProductionElement_c n = (ProductionElement_c) copy();
			n.action = action;
			n.factor = factor;
			return n;
		}
		return this;
	}

	public Node visitChildren(NodeVisitor v) {
		Factor factor = (Factor) this.factor.visit(v);
		SemanticAction action = (SemanticAction) this.action.visit(v);
		return reconstruct(factor, action);
	}

	public ProductionElement setSemanticAction(SemanticAction action) {
		ProductionElement_c n = (ProductionElement_c) copy();
		n.action = action;
		return n;
	}

	public void prettyPrint(CodeWriter w, PrettyPrinter tr) {
		factor.prettyPrint(w, tr);
		w.write(" ");
		action.prettyPrint(w, tr);
	}
	
	@Override
	public void accept(Visitor v) {
		v.visitProductionElement(this);
	}
	
}
